﻿using FlyingEye.Consts;
using FlyingEye.ESApplication.Models.PowderProcess;
using FlyingEye.ESApplication.Services;
using FlyingEye.Middlewares.RateLimiting;
using FlyingEye.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Threading.Tasks;
using Volo.Abp.AspNetCore.Mvc;

namespace FlyingEye.Controllers
{
    /// <summary>
    /// 粉料工序控制器
    /// api 使用基于用户的滑动窗口限流策略
    /// api 授权于 admin 和 me
    /// </summary>
    [ApiController]
    [Route("[controller]/[action]")]
    [EnableRateLimiting(RateLimiterPolicyNames.UserFixedPolicy, LimitingMode = LimitingMode.BaseUser)]
    [Authorize(Roles = $"{UserRoleConsts.ME},{UserRoleConsts.Admin}")]
    public class PowderProcessController : AbpController
    {
        private readonly IPowderProcessService _service;

        private readonly ILogger<PowderProcessController> _logger;

        public PowderProcessController(IPowderProcessService service, ILogger<PowderProcessController> logger)
        {
            _service = service;
            _logger = logger;
        }

        /// <summary>
        /// 记录粉料投料工序搅拌中控报警
        /// </summary>
        [HttpPost]
        public async Task<ActionResult<DefaultActionResult>> RecordMixingEquipmentAlarm(MixingEquipmentAlarm alarm)
        {
            var result = new DefaultActionResult();

            try
            {
                await _service.RecordMixingEquipmentAlarm(alarm);
                result.IsSuccess = true;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "记录粉料工序搅拌设备报警信息出现异常。");
                result.Message = "记录粉料工序搅拌设备报警信息失败，请联系管理员。";
            }

            return result;
        }

        /// <summary>
        /// 记录粉料投料异常报警
        /// </summary>
        [HttpPost]
        public async Task<ActionResult<DefaultActionResult>> RecordPowderFeedingAlarm(PowderFeedingAlarm alarm)
        {
            var result = new DefaultActionResult();

            try
            {
                await _service.RecordPowderFeedingAlarm(alarm);
                result.IsSuccess = true;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "记录粉料投料异常报警信息出现异常。");
                result.Message = "记录粉料投料异常报警信息失败，请联系管理员。";
            }

            return result;
        }

        /// <summary>
        /// 记录粉料投料完成
        /// </summary>
        [HttpPost]
        public async Task<ActionResult<DefaultActionResult>> RecordPowderProcessCompletion(PowderProcessCompletion alarm)
        {
            var result = new DefaultActionResult();

            try
            {
                await _service.RecordPowderProcessCompletion(alarm);
                result.IsSuccess = true;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "记录粉料投料完成信息出现异常。");
                result.Message = "记录粉料投料完成信息失败，请联系管理员。";
            }

            return result;
        }
    }
}
